package com.jess.arms.di.module;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.jess.arms.BuildConfig;
import com.jess.arms.base.App;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.base.delegate.AppDelegate;
import com.jess.arms.base.delegate.AppLifecycles;
import com.jess.arms.base.delegate.sys.LoginReponsBody;
import com.jess.arms.base.delegate.sys.LoginStateUtil;
import com.jess.arms.base.delegate.sys.SysInfo;
import com.jess.arms.integration.ActivityLifecycle;
import com.jess.arms.integration.AppManager;
import com.jess.arms.integration.cache.IntelligentCache;
import com.jess.arms.utils.ArmsUtils;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.tencent.mmkv.MMKV;
//import com.squareup.leakcanary.LeakCanary;
//import com.squareup.leakcanary.RefWatcher;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
//import leakcanary.internal.activity.LeakActivity;
import timber.log.Timber;


/**
 * ================================================
 * 展示 {@link AppLifecycles} 的用法
 * <p>
 * Created by MVPArmsTemplate on 01/07/2019 17:36
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class AppLifecyclesImpl implements AppLifecycles {
    private static Application application;
    public static int  m_version=2;
    //修改日期2021年10月12日 防止内存泄漏
    public static Application getApplication() {
        return application;
    }

    @Override
    public void attachBaseContext(@NonNull Context base) {
        //          MultiDex.install(base);  //这里比 onCreate 先执行,常用于 MultiDex 初始化,插件化框架的初始化
    }

    @Override
    public void onCreate(@NonNull Application application) {
        AppLifecyclesImpl.application = application;
//        if (LeakCanary.isInAnalyzerProcess(application)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
//            return;
//        }
//        if (BuildConfig.LOG_DEBUG) {//Timber初始化
            //Timber 是一个日志框架容器,外部使用统一的Api,内部可以动态的切换成任何日志框架(打印策略)进行日志打印
            //并且支持添加多个日志框架(打印策略),做到外部调用一次 Api,内部却可以做到同时使用多个策略
            //比如添加三个策略,一个打印日志,一个将日志保存本地,一个将日志上传服务器
            Timber.plant(new Timber.DebugTree());
//        MMKV.initialize(application);
            // 如果你想将框架切换为 Logger 来打印日志,请使用下面的代码,如想切换为其他日志框架请根据下面的方式扩展
//                                Logger.addLogAdapter(new AndroidLogAdapter());
            //                    Timber.plant(new Timber.DebugTree() {
            //                        @Override
            //                        protected void log(int priority, String tag, String message, Throwable t) {
            //                            Logger.log(priority, tag, message, t);
            //                        }
            //                    });
//            ButterKnife.setDebug(true);
            /*日志库Logger初始化*/
//            LoggerMaster.initLogger();
            //故障告警  故障复核
//        }
        //初始化tbs
        // 在调用TBS初始化、创建WebView之前进行如下配置
//        HashMap map = new HashMap();
//        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
//        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
//        QbSdk.initTbsSettings(map);
        //LeakCanary 内存泄露检查
        //使用 IntelligentCache.KEY_KEEP 作为 key 的前缀, 可以使储存的数据永久存储在内存中
        //否则存储在 LRU 算法的存储空间中, 前提是 extras 使用的是 IntelligentCache (框架默认使用)
        // TODO: 2019/3/6  LeakCanary框架的使用，搭配Arms(需要时开启)
//        ArmsUtils.obtainAppComponentFromContext(application).extras().put(IntelligentCache.getKeyOfKeep(RefWatcher.class.getName()),LeakCanary.install(application));

        /*仿微信图片选择器*/
//        GlideImageLoader.init();
        /*二维码扫描库*/
//        ZXingLibrary.initDisplayOpinion(application);
//        /*日志库Logger初始化*/
//        LoggerMaster.initLogger();
        /*崩溃日志收集 - 需要权限*/
        // TODO: 2019/3/6  权限
//        CrashHandler.getInstance().init(application);
//        CrashReport.initCrashReport(application, BUGLY_ID, true);


        /*realm数据库配置*/
//        REALM_INIT.initRealmDb(application);

        //读取配置的websocket 地址
//        if (TextUtils.isEmpty(SPUtils.getInstance().getString(ApiConstant.SP_TAG_MESSAGE_SERVER_URL))) {
//            SPUtils.getInstance().put(ApiConstant.SP_TAG_MESSAGE_SERVER_URL, ApiConstant.BASE_MESSAGE_SERVER_URL);//消息 服务器
//        }
//        if (TextUtils.isEmpty(SPUtils.getInstance().getString(ApiConstant.SP_TAG_WS_SERVER_IP))) {
//            SPUtils.getInstance().put(ApiConstant.SP_TAG_WS_SERVER_IP, ApiConstant.BASE_WS_SERVER_IP);//websocket 服务器ip地址
//        }
//        if (TextUtils.isEmpty(SPUtils.getInstance().getString(ApiConstant.SP_TAG_WS_SERVER_PORT))) {
//            SPUtils.getInstance().put(ApiConstant.SP_TAG_WS_SERVER_PORT, ApiConstant.BASE_WS_SERVER_PORT);//websocket 服务器端口号地址
//        }
        /*项目启动时赋值*/
//        SysInfo.serverAddr = SPUtils.getInstance().getString(ApiConstant.SP_TAG_BASE_URL);
//        SysInfo.qxdServerAddr = SPUtils.getInstance().getString(ApiConstant.SP_TAG_QXD_BASE_URL);
//        SysInfo.msgWebURL = SPUtils.getInstance().getString(ApiConstant.SP_TAG_MESSAGE_SERVER_URL);
//        SysInfo.msgSocketAdress = SPUtils.getInstance().getString(ApiConstant.SP_TAG_WS_SERVER_IP);
//        SysInfo.msgSocketPort = SPUtils.getInstance().getString(ApiConstant.SP_TAG_WS_SERVER_PORT);
//
//        //登录状态信息
        SysInfo.emp = (LoginReponsBody) CacheDiskUtils.getInstance().getSerializable("cache_psersoninfo");
        if (ObjectUtils.isNotEmpty(LoginStateUtil.getCachedOkhttpCookie())) {
            SysInfo.cookieStore.putAll(LoginStateUtil.getCachedOkhttpCookie());
        }
//
//        //登录人员 - 工作类型 JT6/JT28/二者都有/没有wroktype字段
//        if (SysInfo.personInfo!=null) {
//            if (ObjectUtils.isEmpty(SysInfo.personInfo.worktype)) {
//                SysInfo.wrokTypeCode = SysInfo.GLY_CODE;
//            } else {
//                if (SysInfo.XCX.equalsIgnoreCase(SysInfo.personInfo.worktype)) {
//                    SysInfo.wrokTypeCode = SysInfo.XCX_CODE;
//                } else if (SysInfo.ZB_ZB.equalsIgnoreCase(SysInfo.personInfo.worktype)||SysInfo.ZB_RJ.equalsIgnoreCase(SysInfo.personInfo.worktype)) {
//                    SysInfo.wrokTypeCode = SysInfo.ZB_CODE;
//                }
//            }
//        }
//
//        //rfid 初始化
//        try {
//            mService = UhfAdapter.getUhfManager(application);
//            if (mService != null) {
//                int status=mService.getStatus();
//                m_version=((status&0xff0000)>>16)&0xff;
//                if(mService.open()){
//                    int b=0;
//                }
//            }
//        } catch (Exception e) {
//            //Toast.makeText(App.this, "dasd", Toast.LENGTH_SHORT).show();
//        }
//        //全局浮窗
//
//        startScan();

    }

    @Override
    public void onTerminate(@NonNull Application application) {

    }




}
